﻿/*--------------------------------------------------
 Creater：Junxuan.Su
 Time：2013-12-9 15:58:42
 Remark : 日志类
--------------------------------------------------*/

using System;
using System.IO;
using System.Text;

namespace HXDNB.Infrastructure
{
    public class Log
    {
        /// <summary>
        /// 记录信息
        /// </summary>
        /// <param name="location">位置</param>
        /// <param name="message">信息</param>
        public static void Info(string location, string message)
        {
            Write(location, "INFO", message);
        }

        /// <summary>
        /// 记录警告
        /// </summary>
        /// <param name="location">位置</param>
        /// <param name="message">信息</param>
        public static void Warning(string location, string message)
        {
            Write(location, "WARNING", message);
        }

        /// <summary>
        /// 记录错误
        /// </summary>
        /// <param name="location">位置</param>
        /// <param name="message">信息</param>
        public static void Error(string location, string message)
        {
            Write(location, "ERROR", message);
        }

        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="location">位置</param>
        /// <param name="type">类型</param>
        /// <param name="message">信息</param>
        public static void Write(string location, string type, string message)
        {
            Write(string.Format("{0}\t<{1}>\t{2}", type, location, message));
        }

        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="content">内容</param>
        public static void Write(string content)
        {
            try
            {
                var folder = Config.Log;
                if (!Directory.Exists(folder))
                    Directory.CreateDirectory(folder);
                var filePath = folder + "//" + DateTime.Today.ToString("yyyyMMdd") + ".txt";
                File.AppendAllText(filePath, string.Format("{0}：{1}\r\n", DateTime.Now, content), Encoding.UTF8);
            }
            catch
            {
              //  throw new Exception("Error on WriteLog :" + content);
            }
        }

    }
}
